<template>
	<view class="authIndex"><button class="getToken" open-type="getUserInfo" @getuserinfo="handlerLogin">授权微信登录</button></view>
</template>

<script>
import { post } from '@/uilts/http.js';
export default {
	data() {
		return {};
	},
	methods: {
		// 目的需要获取用户token,需要调用微信button的开放能力获取参数,encryptedData,rawData,iv,signature
		async handlerLogin(e) {
			console.log(e);
			if (e.detail.errMsg === 'getUserInfo:ok') {
				// 先把用户信息存到本地以后使用
				await uni.setStorage({
					key: 'userInfo',
					data: e.detail.userInfo
				});
				let { cloudID, encryptedData, iv, rawData, signature } = e.detail;
				// 再使用微信的login
				let [loginError, loginRes] = await uni.login();
				let code = loginRes.code;
				let data = { cloudID, encryptedData, iv, rawData, signature, code };
				// 发起请求,获取用户token
				let res = await post('/users/wxlogin', data);
				console.log(res);
				if (res.data.meta.status === 200) {
					// 登录成功,把token,存到本地
					await uni.setStorage({
						key: 'token',
						data: res.data.message.token
					});
					await uni.navigateBack();
					await uni.showToast({
						icon: 'none',
						title: '登录成功'
					});
					// 获取了token,我们需要从定向到支付页面
					
				} else {
					uni.showToast({
						icon: 'none',
						title: '系统开小差了,请重试'
					});
				}
				// }
			} else {
				uni.showToast({
					icon: 'none',
					title: '请先登录哟!'
				});
			}
		}
	}
};
</script>

<style lang="less">
.authIndex {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100vh;
	background-color: #f9f9f9;
	.getToken {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 461rpx;
		height: 88rpx;
		font-size: 36rpx;
		border-radius: 8rpx;
		padding: 0;
		line-height: 1;
		background-color: #07c160;
		color: #fff;
		&::after {
			border: 0;
		}
	}
}
</style>
